/**
 * 
 */
package com.haisen.lee.util;

import java.nio.charset.Charset;

/**
 * @author Haisen Lee
 * 
 */
public class QueryStringTranscoding {

	/**
	 * 
	 */
	private QueryStringTranscoding() {
		super();
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-4-30
	 * 
	 * @param sourceStr
	 * @param sourceCodeFormat
	 * @return
	 */
	public static String TranscodeToUTF8QueryString(String sourceStr,
			String sourceCodeFormat) {
		return Transcode(sourceStr, sourceCodeFormat, "UTF-8");
	}

	/**
	 * 
	 * 
	 * Haisen Lee 2010-4-30
	 * 
	 * @param sourceStr
	 * @param sourceCodeFormat
	 * @param desCodeFormat
	 * @return
	 */
	public static String TranscodeToQueryString(String sourceStr,
			String sourceCodeFormat, String desCodeFormat) {
		return Transcode(sourceStr, sourceCodeFormat, desCodeFormat);
	}

	public static String TranscodeQueryStringToGBK(String sourceStr,
			String sourceCodeFormat, String desCodeFormat) {
		return Transcode(sourceStr, sourceCodeFormat, "GBK");
	}

	public static String TranscodeFromQueryString(String sourceStr,
			String sourceCodeFormat, String desCodeFormat) {
		return Transcode(sourceStr, sourceCodeFormat, desCodeFormat);
	}

	private static String Transcode(String sourceStr, String sourceCodeFormat,
			String desCodeFormat) {
		if (sourceStr != null && sourceStr.length() > 0) {
			return new String(sourceStr.getBytes(Charset
					.forName(sourceCodeFormat)), Charset.forName(desCodeFormat));
		} else {
			return "";
		}
	}

}
